<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book python高级 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>数据库编程 | python高级</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../day16/zongjie.html" />
    
    
    <link rel="prev" href="../day16/dbdesign.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="6.4"
        data-chapter-title="数据库编程"
        data-filepath="day16/pymysql.md"
        data-basepath=".."
        data-revision="Wed Sep 26 2018 22:46:13 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Python高级
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="day11/00.readme.html">
            
                
                    <a href="../day11/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Python语法高级专题1
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="day11/01.GIL.html">
            
                
                    <a href="../day11/01.GIL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        GIL问题
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="day11/02.copy.html">
            
                
                    <a href="../day11/02.copy.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        赋值-浅拷贝-深拷贝
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="day11/03.import.html">
            
                
                    <a href="../day11/03.import.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        import导入模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="day11/04.review.html">
            
                
                    <a href="../day11/04.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="day12/00.readme.html">
            
                
                    <a href="../day12/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        Python语法高级专题2
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="day12/01.mro.html">
            
                
                    <a href="../day12/01.mro.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        多继承和MRO顺序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="day12/02.property.html">
            
                
                    <a href="../day12/02.property.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        property属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="day12/03.magic.html">
            
                
                    <a href="../day12/03.magic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        魔法属性和方法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="day12/04.with.html">
            
                
                    <a href="../day12/04.with.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        with管理上下文
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="day12/05.review.html">
            
                
                    <a href="../day12/05.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="day13/00.readme.html">
            
                
                    <a href="../day13/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Python语法之装饰器
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="day13/01.closure.html">
            
                
                    <a href="../day13/01.closure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        闭包
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="day13/02.decorator.html">
            
                
                    <a href="../day13/02.decorator.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        装饰器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="day13/03.review.html">
            
                
                    <a href="../day13/03.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="day14/00.readme.html">
            
                
                    <a href="../day14/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        MySQL数据库基本操作
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="day14/01.数据库概述.html">
            
                
                    <a href="../day14/01.数据库概述.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        数据库概述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="day14/02.数据类型和约束.html">
            
                
                    <a href="../day14/02.数据类型和约束.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        数据完整性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="day14/03.Navicat.html">
            
                
                    <a href="../day14/03.Navicat.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        图形化客户端Navicat
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="day14/04.命令行客户端.html">
            
                
                    <a href="../day14/04.命令行客户端.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        命令行客户端SQL的使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.4.1" data-path="day14/05.sql_con.html">
            
                
                    <a href="../day14/05.sql_con.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.1.</b>
                        
                        登录和退出数据库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.2" data-path="day14/07.sql_table_struct.html">
            
                
                    <a href="../day14/07.sql_table_struct.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.2.</b>
                        
                        表结构的操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.3" data-path="day14/06.sql_database.html">
            
                
                    <a href="../day14/06.sql_database.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.3.</b>
                        
                        数据库的操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.4" data-path="day14/08.表数据操作.html">
            
                
                    <a href="../day14/08.表数据操作.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.4.</b>
                        
                        表数据的操作
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="day14/09.review.html">
            
                
                    <a href="../day14/09.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        知识总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="day15/00.readme.html">
            
                
                    <a href="../day15/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        MYSQL数据库进阶操作
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="day15/01.练习.html">
            
                
                    <a href="../day15/01.练习.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        练习强化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="day15/02.where条件.html">
            
                
                    <a href="../day15/02.where条件.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        条件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="day15/03.排序查询.html">
            
                
                    <a href="../day15/03.排序查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="day15/04.分页查询.html">
            
                
                    <a href="../day15/04.分页查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        分页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="day15/05.聚合函数.html">
            
                
                    <a href="../day15/05.聚合函数.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        聚合函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="day15/06.分组.html">
            
                
                    <a href="../day15/06.分组.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        分组
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="day15/07.连接.html">
            
                
                    <a href="../day15/07.连接.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="day15/08.自连接.html">
            
                
                    <a href="../day15/08.自连接.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        自连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.9" data-path="day15/09.子查询.html">
            
                
                    <a href="../day15/09.子查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.9.</b>
                        
                        子查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.10" data-path="day15/10.总结.html">
            
                
                    <a href="../day15/10.总结.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.10.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="day16/readme.html">
            
                
                    <a href="../day16/readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        MYSQL数据库编程
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="day16/data.html">
            
                
                    <a href="../day16/data.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        数据准备
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="day16/sql_code.html">
            
                
                    <a href="../day16/sql_code.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        SQL演练
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="day16/dbdesign.html">
            
                
                    <a href="../day16/dbdesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        数据库设计
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="6.4" data-path="day16/pymysql.html">
            
                
                    <a href="../day16/pymysql.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        数据库编程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="day16/zongjie.html">
            
                
                    <a href="../day16/zongjie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="day17/00.readme.html">
            
                
                    <a href="../day17/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        MySQL数据库高级用法
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="day17/01.view.html">
            
                
                    <a href="../day17/01.view.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="day17/02.transaction.html">
            
                
                    <a href="../day17/02.transaction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        事务
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="day17/03.sql_index.html">
            
                
                    <a href="../day17/03.sql_index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        索引
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="day17/04.user.html">
            
                
                    <a href="../day17/04.user.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        用户管理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="day17/05.master_slave.html">
            
                
                    <a href="../day17/05.master_slave.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        主从配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.6" data-path="day17/06.review.html">
            
                
                    <a href="../day17/06.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.6.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="day18/00.readme.html">
            
                
                    <a href="../day18/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        miniweb框架
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="day18/01.框架是什么.html">
            
                
                    <a href="../day18/01.框架是什么.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        框架概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="day18/02.实现框架.html">
            
                
                    <a href="../day18/02.实现框架.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        实现一个简单的框架
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="day18/03.模板.html">
            
                
                    <a href="../day18/03.模板.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        模板替换
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="day18/04.路由.html">
            
                
                    <a href="../day18/04.路由.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        路由概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="day18/05.显示股票数据.html">
            
                
                    <a href="../day18/05.显示股票数据.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        股票收藏夹
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="day18/05.review.html">
            
                
                    <a href="../day18/05.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        复习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >python高级</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="4-&#x6570;&#x636E;&#x5E93;&#x7F16;&#x7A0B;">4. &#x6570;&#x636E;&#x5E93;&#x7F16;&#x7A0B;</h1>
<h2 id="41-&#x5B66;&#x4E60;&#x76EE;&#x6807;">4.1 &#x5B66;&#x4E60;&#x76EE;&#x6807;</h2>
<ol>
<li>&#x80FD;&#x591F;&#x8BF4;&#x51FA; python &#x64CD;&#x4F5C;&#x6570;&#x636E;&#x5E93;&#x7684;&#x6B65;&#x9AA4;</li>
<li>&#x80FD;&#x591F; connect &#x65B9;&#x6CD5;&#x521B;&#x5EFA;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;</li>
<li>&#x80FD;&#x591F;&#x4F7F;&#x7528;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;&#x7684; cursor()&#x65B9;&#x6CD5; &#x521B;&#x5EFA;&#x6E38;&#x6807;&#x5BF9;&#x8C61;</li>
<li>&#x80FD;&#x591F;&#x4F7F;&#x7528;&#x6E38;&#x6807;&#x5BF9;&#x8C61;&#x7684; execute()&#x65B9;&#x6CD5;&#x6267;&#x884C; SQL &#x8BED;&#x53E5;</li>
<li>&#x80FD;&#x591F;&#x4F7F;&#x7528;&#x6E38;&#x6807;&#x5BF9;&#x8C61;&#x7684; close()&#x65B9;&#x6CD5;&#x5173;&#x95ED;&#x6E38;&#x6807;&#x5BF9;&#x8C61;</li>
<li>&#x80FD;&#x591F;&#x4F7F;&#x7528;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;&#x7684; close()&#x65B9;&#x6CD5;&#x5173;&#x95ED;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;</li>
<li>&#x80FD;&#x591F;&#x4F7F;&#x7528;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;&#x7684; commit()&#x65B9;&#x6CD5;&#x5173;&#x95ED;&#x63D0;&#x4EA4;&#x6570;&#x636E;</li>
<li>&#x80FD;&#x591F;&#x4F7F;&#x7528;&#x6E38;&#x6807;&#x5BF9;&#x8C61;&#x7684; fetchall()&#x65B9;&#x6CD5;&#x53D6;&#x51FA;&#x6240;&#x6709;&#x6570;&#x636E;</li>
<li>&#x80FD;&#x591F;&#x4F7F;&#x7528;&#x6E38;&#x6807;&#x5BF9;&#x8C61;&#x7684; fetchone()&#x65B9;&#x6CD5;&#x53D6;&#x51FA;&#x4E00;&#x6761;&#x6570;&#x636E;</li>
<li>&#x80FD;&#x591F;&#x8BF4;&#x51FA;&#x5982;&#x4F55;&#x907F;&#x514D; SQL &#x6CE8;&#x5165;&#x95EE;&#x9898;</li>
</ol>
<hr>
<h2 id="42-&#x6570;&#x636E;&#x5E93;&#x7F16;&#x7A0B;">4.2 &#x6570;&#x636E;&#x5E93;&#x7F16;&#x7A0B;</h2>
<p>&#x4ECE;&#x524D;&#x9762;&#x6211;&#x4EEC;&#x77E5;&#x9053;&#x6570;&#x636E;&#x5E93;&#x6982;&#x5FF5;&#x5305;&#x542B; &#x6570;&#x636E;&#x5E93;&#x6587;&#x4EF6;&#x3001;&#x670D;&#x52A1;&#x5668;&#x548C;&#x6570;&#x636E;&#x5E93;&#x5BA2;&#x6237;&#x7AEF;
&#x5BA2;&#x6237;&#x7AEF;&#x6211;&#x4EEC;&#x4E4B;&#x524D;&#x5DF2;&#x7ECF;&#x7528;&#x8FC7;&#x7684;&#x6709;navicat/mysql-client&#x7B49;&#x7A0B;&#x5E8F;&#x3002;</p>
<blockquote>
<p>&#x95EE;&#x9898;: &#x5982;&#x4F55;&#x4F7F;&#x7528;&#x5BA2;&#x6237;&#x7AEF;&#x5C06;100000&#x884C;&#x6570;&#x636E;&#x63D2;&#x5165;&#x5230;&#x6570;&#x636E;&#x5E93;?</p>
</blockquote>
<p>&#x5927;&#x5BB6;&#x4F1A;&#x53D1;&#x73B0;&#x5982;&#x679C;&#x7528;&#x4E4B;&#x524D;&#x5BA2;&#x6237;&#x7AEF;&#x51E0;&#x4E4E;&#x662F;&#x4E0D;&#x53EF;&#x80FD;&#x5B8C;&#x5168;&#x8FD9;&#x4E2A;&#x4EFB;&#x52A1;&#x7684;, &#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x4E0D;&#x53EF;&#x80FD;&#x53BB;&#x6784;&#x9020;&#x51FA;&#x90A3;&#x4E2A;&#x63D2;&#x5165;10w&#x884C;&#x6570;&#x636E;&#x7684;SQL&#x3002;&#x53EF;&#x662F;&#xFF0C;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x6709;&#x4E00;&#x4E2A;&#x529F;&#x80FD;&#x80FD;&#x591F;&#x63D2;&#x5165;&#x4E00;&#x884C;&#x6570;&#x636E;&#xFF0C;&#x501F;&#x52A9;&#x7A0B;&#x5E8F;&#x5F3A;&#x5927;&#x7684;&#x7279;&#x70B9;-&#x91CD;&#x590D;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x8F7B;&#x800C;&#x6613;&#x4E3E;&#x7684;&#x5C31;&#x5C06;10w&#x884C;&#x6570;&#x636E;&#x6536;&#x5165;&#x9EBE;&#x4E0B;&#x3002;&#x8FD9;&#x79CD;&#x901A;&#x8FC7;&#x4F7F;&#x7528;&#x7A0B;&#x5E8F;&#x4EE3;&#x7801;&#x7684;&#x65B9;&#x5F0F;&#x53BB;&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x901A;&#x8FC7;&#x548C;&#x670D;&#x52A1;&#x5668;&#x8FDB;&#x884C;&#x4EA4;&#x4E92;&#x5B8C;&#x6210;&#x5BF9;&#x6570;&#x636E;&#x5E93;&#x7684;&#x589E;&#x5220;&#x6539;&#x67E5;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x5C31;&#x79F0;&#x4E3A;<strong>&#x6570;&#x636E;&#x5E93;&#x7F16;&#x7A0B;</strong>&#x3002;</p>
<p>&#x800C;&#x6B64;&#x523B;&#x5B66;&#x4E60;&#x7684;pymysql&#x5C31;&#x662F;&#x4E00;&#x79CD;&#x5BA2;&#x6237;&#x7AEF;&#x3002;</p>
<h2 id="43-python-&#x4E2D;&#x64CD;&#x4F5C;-mysql-&#x6B65;&#x9AA4;">4.3 Python &#x4E2D;&#x64CD;&#x4F5C; MySQL &#x6B65;&#x9AA4;</h2>
<p><img src="../assets/day16/&#x6570;&#x636E;&#x5E93;&#x7F16;&#x7A0B;&#x6B65;&#x9AA4;.jpeg" alt=""></p>
<blockquote>
<p>&#x5982;&#x4F55;&#x7406;&#x89E3;&#x8FDE;&#x63A5; connection &#x548C; &#x6E38;&#x6807; cursor
    connection&#x5C31;&#x50CF;&#x662F;&#x8FDE;&#x63A5;&#x51FA;&#x53D1;&#x5730;&#x548C;&#x76EE;&#x7684;&#x5730;&#x7684; &#x9AD8;&#x901F;&#x516C;&#x8DEF; 
    cursor&#x5C31;&#x50CF;&#x662F;&#x5728;&#x9AD8;&#x901F;&#x516C;&#x8DEF;&#x4E0A;&#x7684;&#x8D27;&#x8F66;-&#x62C9;&#x8D27;
    &#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x6E38;&#x6807;&#x5C31;&#x53EF;&#x4EE5;&#x5B8C;&#x6210;&#x5BF9;&#x6570;&#x636E;&#x7684;&#x64CD;&#x4F5C;
    &#x5F53;&#x6211;&#x4EEC;&#x5B8C;&#x6210;&#x64CD;&#x4F5C;&#x5B8C;&#x6210;&#x540E;&#x5C31;&#x53EF;&#x4EE5;&#x505C;&#x4E0B;&#x8D27;&#x8F66;&#xFF0C;&#x7136;&#x540E;&#x516C;&#x8DEF;&#x518D;&#x505C;&#x6B62;&#x4F7F;&#x7528;&#x3002;</p>
</blockquote>
<h2 id="44-pymysql&#x7684;&#x4F7F;&#x7528;">4.4 pymysql&#x7684;&#x4F7F;&#x7528;</h2>
<blockquote>
<p>&#x5F15;&#x5165;&#x6A21;&#x5757;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> pymysql <span class="hljs-keyword">import</span> connect
</code></pre>
</blockquote>
<h3 id="441-connection-&#x5BF9;&#x8C61;">4.4.1 Connection &#x5BF9;&#x8C61;</h3>
<ul>
<li>&#x7528;&#x4E8E;&#x5EFA;&#x7ACB;&#x4E0E;&#x6570;&#x636E;&#x5E93;&#x7684;&#x8FDE;&#x63A5;
  &#x8C03;&#x7528;pymysql&#x6A21;&#x5757;&#x4E2D;&#x7684;connect()&#x65B9;&#x6CD5;</li>
</ul>
<pre><code class="lang-python">conn=connect(&#x53C2;&#x6570;&#x5217;&#x8868;)

* &#x53C2;&#x6570;host&#xFF1A;&#x8FDE;&#x63A5;&#x7684;mysql&#x4E3B;&#x673A;&#xFF0C;&#x5982;&#x679C;&#x672C;&#x673A;&#x662F;<span class="hljs-string">&apos;localhost&apos;</span>
* &#x53C2;&#x6570;port&#xFF1A;&#x8FDE;&#x63A5;&#x7684;mysql&#x4E3B;&#x673A;&#x7684;&#x7AEF;&#x53E3;&#xFF0C;&#x9ED8;&#x8BA4;&#x662F;<span class="hljs-number">3306</span>
* &#x53C2;&#x6570;database&#xFF1A;&#x6570;&#x636E;&#x5E93;&#x7684;&#x540D;&#x79F0;
* &#x53C2;&#x6570;user&#xFF1A;&#x8FDE;&#x63A5;&#x7684;&#x7528;&#x6237;&#x540D;
* &#x53C2;&#x6570;password&#xFF1A;&#x8FDE;&#x63A5;&#x7684;&#x5BC6;&#x7801;
* &#x53C2;&#x6570;charset&#xFF1A;&#x901A;&#x4FE1;&#x91C7;&#x7528;&#x7684;&#x7F16;&#x7801;&#x65B9;&#x5F0F;&#xFF0C;&#x63A8;&#x8350;&#x4F7F;&#x7528;utf8
</code></pre>
<ul>
<li><p>&#x5173;&#x95ED;&#x8FDE;&#x63A5; 
  <code>conn.close()</code></p>
</li>
<li><p>&#x63D0;&#x4EA4;&#x6570;&#x636E;
  <code>conn.commit()</code></p>
</li>
<li><p>&#x64A4;&#x9500;&#x6570;&#x636E;
  <code>conn.rollback()</code></p>
</li>
<li><p>&#x901A;&#x8FC7;&#x8FDE;&#x63A5;&#x83B7;&#x53D6;&#x6E38;&#x6807; 
  <code>cur = conn.cursor()</code>&#x8FD4;&#x56DE;Cursor&#x5BF9;&#x8C61;&#xFF0C;&#x7528;&#x4E8E;&#x6267;&#x884C;sql&#x8BED;&#x53E5;&#x5E76;&#x83B7;&#x5F97;&#x7ED3;&#x679C;</p>
</li>
</ul>
<h3 id="442-cursor&#x6E38;&#x6807;&#x5BF9;&#x8C61;">4.4.2 Cursor&#x6E38;&#x6807;&#x5BF9;&#x8C61;</h3>
<ul>
<li><p>&#x83B7;&#x53D6;Cursor&#x5BF9;&#x8C61;</p>
<pre><code class="lang-python">  <span class="hljs-comment"># &#x8C03;&#x7528;Connection&#x5BF9;&#x8C61;&#x7684;cursor()&#x65B9;&#x6CD5;    </span>
  cur =conn.cursor()
</code></pre>
</li>
</ul>
<blockquote>
<p>&#x76EE;&#x7684;: <strong>&#x6267;&#x884C;sql&#x8BED;&#x53E5;</strong>&#xFF08;&#x4F7F;&#x7528;&#x9891;&#x5EA6;&#x6700;&#x9AD8;&#x7684;&#x8BED;&#x53E5;&#x4E3A;select&#x3001;insert&#x3001;update&#x3001;delete)</p>
</blockquote>
<ul>
<li>&#x4F7F;&#x7528;&#x6E38;&#x6807;&#x6267;&#x884C;SQL&#x8BED;&#x53E5;</li>
</ul>
<p><code>execute(operation [, parameters ])</code>
&#x6267;&#x884C;SQL&#x8BED;&#x53E5;&#xFF0C;<strong>&#x8FD4;&#x56DE;&#x53D7;&#x5F71;&#x54CD;&#x7684;&#x884C;&#x6570;</strong>&#xFF0C;&#x4E3B;&#x8981;&#x7528;&#x4E8E;&#x6267;&#x884C;insert&#x3001;update&#x3001;delete&#x8BED;&#x53E5;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x6267;&#x884C;create&#x3001;alter&#x3001;drop&#x7B49;&#x8BED;&#x53E5;</p>
<ul>
<li>&#x5173;&#x95ED;&#x6E38;&#x6807;
  <code>cur.close()</code></li>
</ul>
<ul>
<li><p>&#x83B7;&#x53D6;&#x7ED3;&#x679C;&#x96C6;&#x4E2D;&#x7684;&#x4E00;&#x6761;</p>
<p>  <code>cur.fetchone()</code>&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5143;&#x7EC4; &#x5F62;&#x5982; (1,&apos;&#x59B2;&#x5DF1;&apos;,18)</p>
</li>
<li><p>&#x83B7;&#x53D6;&#x7ED3;&#x679C;&#x96C6;&#x4E2D;&#x7684;&#x6240;&#x6709;</p>
<p>  <code>cur.fetchall()</code>&#x6267;&#x884C;&#x67E5;&#x8BE2;&#x65F6;&#xFF0C;&#x83B7;&#x53D6;&#x7ED3;&#x679C;&#x96C6;&#x7684;&#x6240;&#x6709;&#x884C;&#xFF0C;&#x4E00;&#x884C;&#x6784;&#x6210;&#x4E00;&#x4E2A;&#x5143;&#x7EC4;&#xFF0C;&#x518D;&#x5C06;&#x8FD9;&#x4E9B;&#x5143;&#x7EC4;&#x88C5;&#x5165;&#x4E00;&#x4E2A;&#x5143;&#x7EC4;&#x8FD4;&#x56DE; &#x5F62;&#x5982;((1,&apos;&#x516C;&#x5B59;&#x79BB;&apos;,20),(2,&apos;&#x59B2;&#x5DF1;&apos;,18))</p>
</li>
</ul>
<h3 id="443-pymysql&#x5B8C;&#x6210;&#x6570;&#x636E;&#x67E5;&#x8BE2;">4.4.3 pymysql&#x5B8C;&#x6210;&#x6570;&#x636E;&#x67E5;&#x8BE2;</h3>
<pre><code class="lang-py"><span class="hljs-keyword">import</span> pymysql

<span class="hljs-comment"># &#x521B;&#x5EFA;&#x548C;&#x6570;&#x636E;&#x5E93;&#x670D;&#x52A1;&#x5668;&#x7684;&#x8FDE;&#x63A5; &#x670D;&#x52A1;&#x5668;&#x5730;&#x5740;&#x3000;&#x3000;&#x3000;&#x7AEF;&#x53E3;&#x3000;&#x3000;&#x3000;&#x3000;&#x7528;&#x6237;&#x540D;&#x3000;&#x3000;&#x3000;&#x3000;&#x3000;&#x5BC6;&#x7801;  &#x6570;&#x636E;&#x5E93;&#x540D; &#x901A;&#x4FE1;&#x4F7F;&#x7528;&#x5B57;&#x7B26;&#x548C;&#x6570;&#x636E;&#x5E93;&#x5B57;&#x7B26;&#x96C6;&#x4E00;&#x81F4;</span>
conn = pymysql.connect(host=<span class="hljs-string">&apos;localhost&apos;</span>, port=<span class="hljs-number">3306</span>, user=<span class="hljs-string">&apos;root&apos;</span>, password=<span class="hljs-string">&apos;mysql&apos;</span>,database=<span class="hljs-string">&apos;python_test_1&apos;</span>, charset=<span class="hljs-string">&apos;utf8&apos;</span>)

<span class="hljs-comment"># &#x83B7;&#x53D6;&#x6E38;&#x6807;</span>
cursor = conn.cursor()

<span class="hljs-comment"># &#x6267;&#x884C;&#xFF33;&#xFF31;&#xFF2C;&#x8BED;&#x53E5; &#x8FD4;&#x56DE;&#x503C;&#x5C31;&#x662F;&#xFF33;&#xFF31;&#xFF2C;&#x8BED;&#x53E5;&#x5728;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x4E2D;&#x5F71;&#x54CD;&#x7684;&#x884C;&#x6570;</span>
sql = <span class="hljs-string">&quot;&quot;&quot;select * from hero;&quot;&quot;&quot;</span>

row_count = cursor.execute(sql)
print(<span class="hljs-string">&quot;&#xFF33;&#xFF31;&#xFF2C;&#x8BED;&#x53E5;&#x6267;&#x884C;&#x5F71;&#x54CD;&#x7684;&#x884C;&#x6570;%d&quot;</span> % row_count)

<span class="hljs-comment"># &#x53D6;&#x51FA;&#x7ED3;&#x679C;&#x96C6;&#x4E2D;&#x4E00;&#x884C;  &#x8FD4;&#x56DE;&#x7684;&#x7ED3;&#x679C;&#x662F;&#x4E00;&#x884C;&#x3000;(1, &apos;&#x59B2;&#x5DF1;&apos;, 2)</span>
<span class="hljs-comment"># print(cursor.fetchone())</span>

<span class="hljs-comment"># &#x53D6;&#x51FA;&#x7ED3;&#x679C;&#x96C6;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x6570;&#x636E;&#x3000;&#x3000;&#x8FD4;&#x56DE;&#x3000;((&#x4E00;&#x884C;&#x6570;&#x636E;),(),())</span>
<span class="hljs-comment"># ((1, &apos;&#x59B2;&#x5DF1;&apos;, 2), (2, &apos;&#x674E;&#x767D;&apos;, 1), (3, &apos;&#x7A0B;&#x54AC;&#x91D1;&apos;, 3), (4, &apos;&#x4E9A;&#x745F;&apos;, 5), (5, &apos;&#x8346;&#x8F72;&apos;, 99))</span>
<span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> cursor.fetchall():
    print(line)

<span class="hljs-comment"># &#x5173;&#x95ED;&#x6E38;&#x6807;</span>
cursor.close()

<span class="hljs-comment"># &#x5173;&#x95ED;&#x8FDE;&#x63A5;</span>
conn.close()
</code></pre>
<h3 id="444-pymysql&#x5B8C;&#x6210;&#x5BF9;&#x6570;&#x636E;&#x5E93;&#x7684;&#x589E;&#x5220;&#x6539;">4.4.4 pymysql&#x5B8C;&#x6210;&#x5BF9;&#x6570;&#x636E;&#x5E93;&#x7684;&#x589E;&#x5220;&#x6539;</h3>
<pre><code class="lang-py"><span class="hljs-keyword">import</span> pymysql

conn = pymysql.connect(host=<span class="hljs-string">&apos;127.0.0.1&apos;</span>,port=<span class="hljs-number">3306</span>,user=<span class="hljs-string">&apos;root&apos;</span>,password=<span class="hljs-string">&apos;mysql&apos;</span>,db=<span class="hljs-string">&apos;python_test_1&apos;</span>, charset=<span class="hljs-string">&apos;utf8&apos;</span>)

<span class="hljs-comment"># &#x901A;&#x8FC7;&#x8FDE;&#x63A5;&#x83B7;&#x53D6;&#x6E38;&#x6807;</span>
cursor = conn.cursor()


<span class="hljs-comment"># sql = &quot;delete from hero where id = 5;&quot;</span>
<span class="hljs-comment"># sql = insert into hero (name) values (&apos;&#x897F;&#x90E8;&#x5927;&#x574F;&#x86CB;&#x5B59;&#x609F;&#x7A7A;&apos;);</span>
sql = <span class="hljs-string">&quot;update hero set kongfuid=444 where id = 4;&quot;</span>

row_count = cursor.execute(sql)
print(<span class="hljs-string">&quot;&#xFF33;&#xFF31;&#xFF2C;&#x8BED;&#x53E5;&#x6267;&#x884C;&#x5F71;&#x54CD;&#x7684;&#x884C;&#x6570;%d&quot;</span> % row_count)

<span class="hljs-comment"># &#x63D0;&#x4EA4;&#x6570;&#x636E;&#x5230;&#x6570;&#x636E;&#x5E93;</span>
<span class="hljs-comment"># conn.commit()</span>

<span class="hljs-comment"># &#x56DE;&#x6EDA;&#x6570;&#x636E;&#x5230;&#x4EC0;&#x4E48;&#x90FD;&#x4E0D;&#x505A;&#x7684;&#x72B6;&#x6001; &#x5373;&#x64A4;&#x9500;&#x521A;&#x521A;&#x7684;&#x4FEE;&#x6539;</span>
conn.rollback()

<span class="hljs-comment"># &#x5173;&#x95ED;&#x6E38;&#x6807;&#x548C;&#x8FDE;&#x63A5;</span>
cursor.close()
conn.close()
</code></pre>
<blockquote>
<p>&#x5173;&#x4E8E;&#x63D0;&#x4EA4;commit
    commit&#x5C06;&#x4FEE;&#x6539;&#x63D0;&#x4EA4;&#x5230;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x4FDD;&#x5B58;&#x4FEE;&#x6539;</p>
<p>&#x6CE8;&#x610F;<strong>pymysql&#x4E2D;&#x6570;&#x636E;&#x9700;&#x8981;&#x624B;&#x52A8;&#x63D0;&#x4EA4;commit&#x624D;&#x80FD;&#x4FDD;&#x5B58;&#x5230;&#x6570;&#x636E;&#x5E93;&#x4E2D;</strong></p>
</blockquote>
<h3 id="445-&#x53C2;&#x6570;&#x5316;&#x5217;&#x8868;&#x9632;&#x6B62;sql&#x6CE8;&#x5165;">4.4.5 &#x53C2;&#x6570;&#x5316;&#x5217;&#x8868;&#x9632;&#x6B62;SQL&#x6CE8;&#x5165;</h3>
<blockquote>
<p>&#x4EC0;&#x4E48;&#x662F;SQL&#x6CE8;&#x5165;
    &#x4EA7;&#x751F;&#x539F;&#x56E0;: &#x540E;&#x53F0;&#x5C06;&#x7528;&#x6237;&#x63D0;&#x4EA4;&#x7684;&#x5E26;&#x6709;&#x6076;&#x610F;&#x7684;&#x6570;&#x636E;&#x548C;SQL&#x8FDB;&#x884C;&#x5B57;&#x7B26;&#x4E32;&#x65B9;&#x5F0F;&#x7684;&#x62FC;&#x63A5;&#xFF0C;&#x4ECE;&#x800C;&#x5F71;&#x54CD;&#x4E86;SQL&#x8BED;&#x53E5;&#x7684;&#x8BED;&#x4E49;&#xFF0C;&#x6700;&#x7EC8;&#x4EA7;&#x751F;&#x6570;&#x636E;&#x6CC4;&#x9732;&#x7684;&#x73B0;&#x8C61;&#x3002;
    &#x5982;&#x679C;&#x9632;&#x6B62;: sql&#x8BED;&#x53E5;&#x7684;&#x53C2;&#x6570;&#x5316;, &#x5C06;SQL&#x8BED;&#x53E5;&#x7684;&#x6240;&#x6709;&#x6570;&#x636E;&#x53C2;&#x6570;&#x5B58;&#x5728;&#x4E00;&#x4E2A;&#x5217;&#x8868;&#x4E2D;&#x4F20;&#x9012;&#x7ED9;execute&#x51FD;&#x6570;&#x7684;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;</p>
<p>&#x6CE8;&#x610F;</p>
<pre><code>* &#x6B64;&#x5904;&#x4E0D;&#x540C;&#x4E8E;python&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x683C;&#x5F0F;&#x5316;&#xFF0C;&#x5FC5;&#x987B;&#x5168;&#x90E8;&#x4F7F;&#x7528;%s&#x5360;&#x4F4D;
* &#x6240;&#x6709;&#x53C2;&#x6570;&#x6240;&#x9700;&#x5360;&#x4F4D;&#x7B26;&#x5916;&#x4E0D;&#x9700;&#x8981;&#x52A0;&#x5F15;&#x53F7;
</code></pre></blockquote>
<pre><code class="lang-py">
<span class="hljs-keyword">from</span> pymysql <span class="hljs-keyword">import</span> connect

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>

    find_name = input(<span class="hljs-string">&quot;&#x8BF7;&#x8F93;&#x5165;&#x7269;&#x54C1;&#x540D;&#x79F0;&#xFF1A;&quot;</span>)

    <span class="hljs-comment"># &#x521B;&#x5EFA;Connection&#x8FDE;&#x63A5;</span>
    conn = connect(host=<span class="hljs-string">&apos;localhost&apos;</span>,port=<span class="hljs-number">3306</span>,user=<span class="hljs-string">&apos;root&apos;</span>,password=<span class="hljs-string">&apos;mysql&apos;</span>,database=<span class="hljs-string">&apos;jing_dong&apos;</span>,charset=<span class="hljs-string">&apos;utf8&apos;</span>)
    <span class="hljs-comment"># &#x83B7;&#x5F97;Cursor&#x5BF9;&#x8C61;</span>
    cs1 = conn.cursor()


    <span class="hljs-comment"># # &#x975E;&#x5B89;&#x5168;&#x7684;&#x65B9;&#x5F0F;</span>
    <span class="hljs-comment"># # &#x8F93;&#x5165; &quot; or 1=1 or &quot;   (&#x53CC;&#x5F15;&#x53F7;&#x4E5F;&#x8981;&#x8F93;&#x5165;)</span>
    <span class="hljs-comment"># sql = &apos;select * from goods where name=&quot;%s&quot;&apos; % find_name</span>
    <span class="hljs-comment"># print(&quot;&quot;&quot;sql===&gt;%s&lt;====&quot;&quot;&quot; % sql)</span>
    <span class="hljs-comment"># # &#x6267;&#x884C;select&#x8BED;&#x53E5;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x53D7;&#x5F71;&#x54CD;&#x7684;&#x884C;&#x6570;&#xFF1A;&#x67E5;&#x8BE2;&#x6240;&#x6709;&#x6570;&#x636E;</span>
    <span class="hljs-comment"># count = cs1.execute(sql)</span>

    <span class="hljs-comment"># &#x5B89;&#x5168;&#x7684;&#x65B9;&#x5F0F;</span>
    <span class="hljs-comment"># &#x6784;&#x9020;&#x53C2;&#x6570;&#x5217;&#x8868; </span>
    params = [find_name]
    <span class="hljs-comment"># &#x6267;&#x884C;select&#x8BED;&#x53E5;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x53D7;&#x5F71;&#x54CD;&#x7684;&#x884C;&#x6570;&#xFF1A;&#x67E5;&#x8BE2;&#x6240;&#x6709;&#x6570;&#x636E;</span>
    count = cs1.execute(<span class="hljs-string">&apos;select * from goods where name=%s&apos;</span>, params)
    <span class="hljs-comment"># &#x6CE8;&#x610F;&#xFF1A;</span>
    <span class="hljs-comment"># &#x5982;&#x679C;&#x8981;&#x662F;&#x6709;&#x591A;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x9700;&#x8981;&#x8FDB;&#x884C;&#x53C2;&#x6570;&#x5316;</span>
    <span class="hljs-comment"># &#x90A3;&#x4E48;params = [&#x6570;&#x503C;1, &#x6570;&#x503C;2....]&#xFF0C;&#x6B64;&#x65F6;sql&#x8BED;&#x53E5;&#x4E2D;&#x6709;&#x591A;&#x4E2A;%s&#x5373;&#x53EF; </span>

    <span class="hljs-comment"># &#x6253;&#x5370;&#x53D7;&#x5F71;&#x54CD;&#x7684;&#x884C;&#x6570;</span>
    print(count)
    <span class="hljs-comment"># &#x83B7;&#x53D6;&#x67E5;&#x8BE2;&#x7684;&#x7ED3;&#x679C;</span>
    <span class="hljs-comment"># result = cs1.fetchone()</span>
    result = cs1.fetchall()
    <span class="hljs-comment"># &#x6253;&#x5370;&#x67E5;&#x8BE2;&#x7684;&#x7ED3;&#x679C;</span>
    print(result)
    <span class="hljs-comment"># &#x5173;&#x95ED;Cursor&#x5BF9;&#x8C61;</span>
    cs1.close()
    <span class="hljs-comment"># &#x5173;&#x95ED;Connection&#x5BF9;&#x8C61;</span>
    conn.close()

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    main()
</code></pre>
<h2 id="45-&#x603B;&#x7ED3;">4.5 &#x603B;&#x7ED3;</h2>
<ol>
<li><p>&#x80FD;&#x591F;&#x8BF4;&#x51FA; python &#x64CD;&#x4F5C;&#x6570;&#x636E;&#x5E93;&#x7684;&#x6B65;&#x9AA4;</p>
<p> &#x521B;&#x5EFA;&#x8FDE;&#x63A5;&#x5BF9;&#x8C61;
 &#x521B;&#x5EFA;&#x6E38;&#x6807;&#x5BF9;&#x8C61;
 &#x6267;&#x884C;SQL&#x8BED;&#x53E5;&#x64CD;&#x4F5C;
 &#x5173;&#x95ED;&#x6E38;&#x6807;
 &#x5173;&#x95ED;&#x8FDE;&#x63A5;</p>
</li>
<li><p>SQL&#x6CE8;&#x5165;&#x95EE;&#x9898;
 &#x5C06;SQL&#x8BED;&#x53E5;&#x7684;&#x6240;&#x6709;&#x6570;&#x636E;&#x53C2;&#x6570;&#x5B58;&#x5728;&#x4E00;&#x4E2A;&#x5217;&#x8868;&#x4E2D;&#x4F20;&#x9012;&#x7ED9;execute&#x51FD;&#x6570;&#x7684;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;</p>
</li>
</ol>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../day16/dbdesign.html" class="navigation navigation-prev " aria-label="Previous page: 数据库设计"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../day16/zongjie.html" class="navigation navigation-next " aria-label="Next page: 总结"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book python高级 -->
</html>
